/*
 * Connection.java
 *
 * Created on September 24, 2004, 3:26 PM
 */

package com.absisgroup.nsc.utilities;

import java.sql.*;
import java.util.Properties;

/**
 *
 * @author  hue
 */
public class DBConnection implements java.sql.Connection {
    private java.sql.PreparedStatement currentPstmt;
    public static long sqlId = 0;
    private java.sql.Connection con;
    private String sqlLogfile; 
    private boolean bCancel;
    private String sessionId;
    
    /** Creates a new instance of Connection */
    public DBConnection(final java.sql.Connection _con, final String _sqlLogfile) {
        con = _con;
        sqlLogfile = _sqlLogfile;
        bCancel = false;
        final StringBuffer sb = new StringBuffer("transactionId=");
        sb.append(this.hashCode());
        sb.append(",session_usage=").append(System.currentTimeMillis());
        sessionId = sb.toString();
    }

    public void cancel() {
        bCancel = true;
    }
    
    private void checkCancelStatus() throws java.sql.SQLException {
        if(bCancel) {
            throw new DBCancelException();
        }
    }
    
    @Override
    public java.sql.Statement createStatement() throws java.sql.SQLException {
        checkCancelStatus();
        return con.createStatement();
    }
    
    @Override
    public java.sql.Statement createStatement(final int resultSetType, final int resultSetConcurrency) throws java.sql.SQLException {
        checkCancelStatus();
        return con.createStatement(resultSetType,resultSetConcurrency);
    }

    @Override
    public java.sql.Statement createStatement(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws java.sql.SQLException {
        checkCancelStatus();
        return con.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability);
    }

    @Override
    public boolean getAutoCommit() throws java.sql.SQLException {
        return con.getAutoCommit();
    }
    
    @Override
    public String getCatalog() throws java.sql.SQLException {
        return con.getCatalog();
    }

    @Override
    public int getHoldability() throws java.sql.SQLException {
        return con.getHoldability();
    }

    @Override
    public java.sql.DatabaseMetaData getMetaData() throws java.sql.SQLException {
        checkCancelStatus();
        return con.getMetaData();
    }
    
    @Override
    public int getTransactionIsolation() throws java.sql.SQLException {
        return con.getTransactionIsolation();
    }
    
    @Override
    public java.util.Map getTypeMap() throws java.sql.SQLException {
        checkCancelStatus();
        return con.getTypeMap();
    }
    
    @Override
    public java.sql.SQLWarning getWarnings() throws java.sql.SQLException {
        return con.getWarnings();
    }
    
    @Override
    public boolean isClosed() throws java.sql.SQLException {
        return con.isClosed();
    }
    
    @Override
    public boolean isReadOnly() throws java.sql.SQLException {
        return con.isReadOnly();
    }
    
    @Override
    public String nativeSQL(final String sql) throws java.sql.SQLException {
        return con.nativeSQL(sql);
    }
    
    @Override
    public java.sql.CallableStatement prepareCall(final String sql) throws java.sql.SQLException {
        checkCancelStatus();
        final DBCallableStatement stmt = new DBCallableStatement(con.prepareCall(sql),sql,sqlLogfile,sessionId);
        return stmt;
    }
    
    @Override
    public java.sql.CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) throws java.sql.SQLException {
        checkCancelStatus();
        final DBCallableStatement stmt = new DBCallableStatement(con.prepareCall(sql,resultSetType,resultSetConcurrency),sql,sqlLogfile,sessionId);
        return stmt;
    }

    @Override
    public java.sql.CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws java.sql.SQLException {
        checkCancelStatus();
        final DBCallableStatement stmt = new DBCallableStatement(con.prepareCall(sql,resultSetType,resultSetConcurrency,resultSetHoldability),sql,sqlLogfile,sessionId);
        return stmt;
    }

    @Override
    public java.sql.PreparedStatement prepareStatement(final String sql) throws java.sql.SQLException {
        checkCancelStatus();
        final DBPreparedStatement pStmt = new DBPreparedStatement(con.prepareStatement(sql),sql,sqlLogfile,sessionId);
        return pStmt;
    }

    @Override
    public java.sql.PreparedStatement prepareStatement(final String sql, final String[] columnNames) throws java.sql.SQLException {
        checkCancelStatus();
        final DBPreparedStatement pStmt = new DBPreparedStatement(con.prepareStatement(sql,columnNames),sql,sqlLogfile,sessionId);
        return pStmt;
    }

    @Override
    public java.sql.PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws java.sql.SQLException {
        checkCancelStatus();
        final DBPreparedStatement pStmt = new DBPreparedStatement(con.prepareStatement(sql,autoGeneratedKeys),sql,sqlLogfile,sessionId);
        return pStmt;
    }

    @Override
    public java.sql.PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) throws java.sql.SQLException {
        checkCancelStatus();
        final DBPreparedStatement pStmt = new DBPreparedStatement(con.prepareStatement(sql,columnIndexes),sql,sqlLogfile,sessionId);
        return pStmt;
    }

    @Override
    public java.sql.PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) throws java.sql.SQLException {
        checkCancelStatus();
        final DBPreparedStatement pStmt = new DBPreparedStatement(con.prepareStatement(sql,resultSetType,resultSetConcurrency),sql,sqlLogfile,sessionId);
        return pStmt;
    }

    @Override
    public java.sql.PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws java.sql.SQLException {
        checkCancelStatus();
        final DBPreparedStatement pStmt = new DBPreparedStatement(con.prepareStatement(sql,resultSetType,resultSetConcurrency,resultSetHoldability),sql,sqlLogfile,sessionId);
        return pStmt;
    }

    @Override
    public java.sql.Savepoint setSavepoint() throws java.sql.SQLException {
        return con.setSavepoint();
    }

    @Override
    public java.sql.Savepoint setSavepoint(final String name) throws java.sql.SQLException {
        return con.setSavepoint(name);
    }

    @Override
    public void setTransactionIsolation(final int level) throws java.sql.SQLException {
        con.setTransactionIsolation(level);
    }
    
    @Override
    public void setTypeMap(final java.util.Map map) throws java.sql.SQLException {
        con.setTypeMap(map);
    }
    
    @Override
    public void clearWarnings() throws java.sql.SQLException {
        con.clearWarnings();
    }
    
    @Override
    public void close() throws java.sql.SQLException {
        con.close();
        this.currentPstmt = null;
    }
    
    @Override
    public void commit() throws java.sql.SQLException {
        con.commit();
        saveTransactionInfo("commit()");
    }

    @Override
    public void setAutoCommit(final boolean autoCommit) throws java.sql.SQLException {
        con.setAutoCommit(autoCommit);
    }

    @Override
    public void releaseSavepoint(final java.sql.Savepoint savepoint) throws java.sql.SQLException {
        con.releaseSavepoint(savepoint);
    }

    @Override
    public void rollback() throws java.sql.SQLException {
        con.rollback();
        saveTransactionInfo("rollback()");
    }

    @Override
    public void rollback(final java.sql.Savepoint savepoint) throws java.sql.SQLException {
        con.rollback(savepoint);
    }

    @Override
    public void setCatalog(final String catalog) throws java.sql.SQLException {
        con.setCatalog(catalog);
    }

    @Override
    public void setHoldability(final int holdability) throws java.sql.SQLException {
        con.setHoldability(holdability);
    }

    @Override
    public void setReadOnly(final boolean readOnly) throws java.sql.SQLException {
        con.setReadOnly(readOnly);
    }
    
    public java.sql.Connection getConnection() {
        return con;
    }
    
    private void saveTransactionInfo(final String info) {
        if(sqlLogfile == null) {
            return;
        }
        try {
            final StringBuffer sb = new StringBuffer("\r\n-- [");
            sb.append(sessionId);
            sb.append("] [");
            sb.append(info);
            sb.append(']');
            Util.writeFile(sqlLogfile, sb.toString().getBytes(), true);
        }
        catch(Exception ex) {
            sqlLogfile = null;
        }
    }
    
    public String getSqlLogfile() {
        return sqlLogfile;
    }
    
    public java.sql.PreparedStatement prepareCustomStatement(final String sql) throws java.sql.SQLException {
        this.currentPstmt = prepareStatement(sql);
        return this.currentPstmt;
    }

    public java.sql.PreparedStatement getActiveStatement() {
        return this.currentPstmt;
    }

    @Override
    public Clob createClob() throws SQLException {
        return con.createClob();
    }

    @Override
    public Blob createBlob() throws SQLException {
        return con.createBlob();
    }

    @Override
    public NClob createNClob() throws SQLException {
        return con.createNClob();
    }

    @Override
    public SQLXML createSQLXML() throws SQLException {
        return con.createSQLXML();
    }

    @Override
    public boolean isValid(int timeout) throws SQLException {
        return con.isValid(timeout);
    }

    @Override
    public void setClientInfo(String name, String value) throws SQLClientInfoException {
        con.setClientInfo(name, value);
    }

    @Override
    public void setClientInfo(Properties properties) throws SQLClientInfoException {
        con.setClientInfo(properties);
    }

    @Override
    public String getClientInfo(String name) throws SQLException {
        return con.getClientInfo(name);
    }

    @Override
    public Properties getClientInfo() throws SQLException {
        return con.getClientInfo();
    }

    @Override
    public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
        return con.createArrayOf(typeName, elements);
    }

    @Override
    public Struct createStruct(String typeName, Object[] attributes) throws SQLException {
        return con.createStruct(typeName, attributes);
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return con.unwrap(iface);
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return con.isWrapperFor(iface);
    }
}
